from PIL import Image


class ESPPicturePixe:
    def __init__(self):
        pass

    def bmp_to_binary_data(self,bmp_path, threshold=128):
        """
        将BMP图像转换为0和1数据

        :param bmp_path: BMP图像文件的路径
        :param threshold: 用于二值化的阈值，默认128
        :return: 二维列表形式的0和1数据
        """
        image = Image.open(bmp_path)
        width, height = image.size

        assert width == 128 ,  "图片宽度必须是128"
        assert height == 64 ,  "图片高度必须是64"

        binary_data = []

        point_arr = []

        for y in range(height):
            row_data = []
            for x in range(width):
                pixel = image.getpixel((x, y))

                if pixel > threshold:
                    row_data.append("-")
                else:
                    row_data.append("1")
                    point_arr.append([x,y])

            binary_data.append(row_data)
        print(point_arr)
        return point_arr

if __name__ == '__main__':
    esp_picture_object = ESPPicturePixe()
    esp_picture_object.bmp_to_binary_data('./face_bmp/face_taohua.bmp')